package com.cainiaoshixi.admin.controller;

import com.cainiaoshixi.admin.common.Result;
import com.cainiaoshixi.admin.entity.Users;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Chy
 * @Description: 登录相关
 * @Date: Created at 20:09 2018/2/7
 */
@Controller
public class LoginController {
    @RequestMapping("/login")
    public String loginIndex(){
        return "login";
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Result userLogin(Users user, HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        Result result = new Result();
        try{
            subject.login(new UsernamePasswordToken(user.getName(), user.getPassword()));
        }catch(AuthenticationException ex){
            result.setSuccess(false);
        }
        return result;
    }

}